package com.vlong.town.controller.client.Goods;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.goods.GoodsInfo;
import com.vlong.town.service.goods.GoodsInfoService;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * 一村一品控制器
 * @author 51088
 */
@Controller
@RequestMapping("/client/goods")
public class GoodsClientController extends BaseController{
	
	@Autowired
	private GoodsInfoService  goodsInfoService;//一寸一品控制器
	
	
	/**
	 * 跳转一村一品页面
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/list")
    public String gotoGoodsInfoPage(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize){
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("create_time DESC");
        List<GoodsInfo> goodsInfoList = this.goodsInfoService.queryList(null, null, null, null,null);
        PageInfo<GoodsInfo> pageInfo = new PageInfo<GoodsInfo>(goodsInfoList);
        PageTag<GoodsInfo> pt = new PageTag<GoodsInfo>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        for(GoodsInfo goodsInfo : goodsInfoList) {
        	goodsInfo.setThumbnail(request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + goodsInfo.getThumbnail());
            goodsInfo.setContent(DataUtil.delHTMLTag(goodsInfo.getContent(), 0));
            Long createTime = goodsInfo.getCreateTime();
        	SimpleDateFormat  sdf=new  SimpleDateFormat("yyyy-MM-dd");
        	Date date = new  Date(createTime);
        	String format = sdf.format(date);
        	String createDate="["+format+"]";
        	goodsInfo.setCreateDate(createDate);
        }
        request.setAttribute("goodsInfoList", goodsInfoList);
        //获取SESSION信息
  		String telphone = getTelphone();
  		request.setAttribute("telphone", telphone);
        return "client/goods/list_goods";
    }
	
	
	/**
	 * 进去详情页面
	 * @param goodsId
	 * @return
	 */
	@RequestMapping("/detail/{goodsId}")
	public String detail(@PathVariable(required=false, value="goodsId") String goodsId){
		GoodsInfo goods = goodsInfoService.queryById(Long.parseLong(goodsId));
		request.setAttribute("goods", goods);
		 //获取SESSION信息
  		String telphone = getTelphone();
  		request.setAttribute("telphone", telphone);
		return "client/goods/detail_goods";
	}
	
	
	
	
	
	

}
